package com.retch.wenchengmao.recodeauthutils;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import com.retch.wenchengmao.recodeauthutils.fragment.FourFragment;
import com.retch.wenchengmao.recodeauthutils.fragment.OneFragment;
import com.retch.wenchengmao.recodeauthutils.fragment.ThreeFragment;
import com.retch.wenchengmao.recodeauthutils.fragment.TwoFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * author:wenchengmao
 * time:  2017/3/27.
 * email: wenchengmao@yeah.net
 */
//顶部导航栏的封装类
public class MainTopFragmentActivity extends AppCompatActivity {
    private TabLayout tabLayout=null;
    private ViewPager viewPager=null;
    private List<Fragment> list;
    private MyAdapter adapter;
    private String[] titles = {"页面1", "页面2", "页面3","页面4"};
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_top_fragment);
        initView();
        getView();
    }

    public void initView()
    {
        tabLayout= (TabLayout) findViewById(R.id.tablayout);
        viewPager =(ViewPager) findViewById(R.id.viewpager);
    }

    public void getView()
    {
        //页面，数据源
        list = new ArrayList<>();
        list.add(new OneFragment());
        list.add(new TwoFragment());
        list.add(new ThreeFragment());
        list.add(new FourFragment());
        //ViewPager的适配器
        adapter = new MyAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        //绑定
        tabLayout.setupWithViewPager(viewPager);
    }

    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        //重写这个方法，将设置每个Tab的标题
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }

}
